package com.example.microinvestweb.controller.user;
import com.example.microinvestcommon.entity.Users;
import com.example.microinvestcommon.pojo.Result;
import com.example.microinvestcommon.util.SnowflakeIdGeneratorUtil;
import com.example.microinvestuser.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//此Controller用于用户的注册，修改信息，更新信息
@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;
    private final SnowflakeIdGeneratorUtil snowflakeIdGeneratorUtil;

    @Autowired
    public UserController(UserService userService,SnowflakeIdGeneratorUtil snowflakeIdGeneratorUtil){
        this.userService=userService;
        this.snowflakeIdGeneratorUtil=snowflakeIdGeneratorUtil;
    }


    //用户注册
    @PostMapping("/register")
    public Result register(String userName,String password,String email) {
        Users user=new Users(userName,password,email,snowflakeIdGeneratorUtil);
        //**由前端判断是否为空**
        Result result=userService.registerUserService(user);
        return result;
    }

    //用户注册发送邮箱
    @PostMapping("/register/sendCode")
    public Result<String> registerEmail(String email){
        Result result=userService.registerEmail(email);
        return result;
    }

    //用户注册邮箱验证
    @PostMapping("/register/identifying")
    public Result<String> registerIdentifyingCode(String email,String code){
        Result result=userService.registerIdentifyingCode(email,code);
        return result;
    }

    //用户修改密码
    @PatchMapping("/updatePwdWithOldPwd")
    public Result<String> updateWithOldPwd(String oldPassword,String newPassword) {
        //由前端保存用户临时信息，返回当前用户用户名，输入旧密码以及新密码
        Result result = userService.updateWithOldPwdService(oldPassword, newPassword);
        return result;
    }


    //用户通过邮箱修改密码(忘记密码，第一步发送邮箱)
    @PostMapping("/updatePwdWithEmailStepOne")
    public Result<String> updatePwdWithEmailStepOne(String email){
        Result result=userService.updatePwdWithEmailStepOne(email);
        return result;
    }

    //用户通过邮箱修改密码(忘记密码，第二步验证邮箱验证码)
    @PostMapping("/updatePwdWithEmailStepTwo")
    public Result<String> updatePwdWithEmailStepTwo(String email,String code){
        Result result=userService.updatePwdWithEmailStepTwo(email,code);
        return result;
    }

    //用户通过邮箱修改密码(忘记密码，第三步修改密码)
    @PatchMapping("/updatePwdWithEmailStepThree")
    public Result<String> updatePwdWithEmailStepThree(String newPwd){
        Result result=userService.updatePwdWithEmailStepThree(newPwd);
        return result;
    }

    //用户进行注销操作（同时适用于管理员删除用户）
    @DeleteMapping("/deleteUser")
    public Result<String> deleteUser(Long userId){
        Result result=userService.deleteUser(userId);
        return result;
    }



}
